import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 10 - 10
 * Time: 17:17
 */
//牛客 Fibonacci数列
public class Solution2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int f1 = 0;
            int f2 = 1;
            while(f2 < n) {
                int f3 = f1 + f2;
                f1 = f2;
                f2 = f3;
            }
            //跳出while循环后f1<n<=f2
            int min = Math.min(n - f1, f2 - n);
            System.out.println(min);
        }
    }
}
